clear all

set more off

qui: cd 
* change directory to the location of replication folder on your computer

******************************
* Download CPI data from FRED
******************************
freduse CPIAUCSL, clear
gen dateq = qofd(daten)
gen YYYYQ = year(daten)*10 + quarter(daten)
collapse (mean) CPI YYYYQ, by (dateq)
tsset dateq
gen lcpi = 100*log(CPI)
gen inflation = lcpi - L4.lcpi
gen f_inflation = F4.lcpi - lcpi
keep if dateq >= yq(1978,1)
keep YYYYQ inflation f_inflation
export delimited using "data/inflation.csv", replace


********************************************
* construct inflation expectation by groups
********************************************
use "msc.dta", clear
qui gen infl1e = PX1 if inrange(PX1, -50, 90) & BIRTHY < . & inrange(AGE, 20, 85)
qui gen infl5e = PX5 if inrange(PX5, -50, 90) & BIRTHY < . & inrange(AGE, 20, 85)

qui gen educg = 1 if inrange(EDUC, 1, 4)		//no college
qui replace educg = 3 if inrange(EDUC, 5, 6)	//college or above

gen year = int(YYYYQ/10)
egen income_m = median(INCOME), by(year)
gen incomeg = (INCOME > income_m)
replace incomeg = . if INCOME == .

qui gen cohortg = int(BIRTHY/20)*20

foreach group in educg incomeg cohortg{
	egen strata_`group' = group(`group' YYYYQ)
	by strata_`group', sort: egen px1_`group' = mean(infl1e)
	by strata_`group', sort: egen px5_`group' = mean(infl5e)
}

tempfile msc
save `msc'

foreach group in educg {
	use `msc', clear
	drop if `group' == .
	qui keep if px1_`group' < .
	qui keep YYYYQ `group' px1_`group' px5_`group'
	duplicates drop 
	reshape wide px1_`group' px5_`group', i(YYYYQ) j(`group')
	sort YYYYQ
	export delimited using "data/agg_px`group'.csv", replace
}

